<?php
class UsersController extends AppController {

    public $helpers = array('Form','Pagination');

    public function admin_index() {
        $this->User->recursive = 2;
        $this->set('users',$this->paginate());
    }

    public function admin_add() {
        if(!empty($this->data)) {
            if($this->User->save_user($this->data)) {
                $this->Session->setFlash(__('The User has been saved',true));
                $this->redirect(array('action'=>'index'),null,true);
            } else {
                $this->Session->setFlash(__('The User could not be saved. Please, try again.',true));
            }
        }
        $this->set('groups',$this->User->Group->find('list'));
    }

    public function admin_change_password($id) {
        if(!empty($this->data)) {
            if($this->User->save_user($this->data)) {
                $this->Session->setFlash(__('The Password has been changed',true));
                $this->redirect(array('action'=>'index'),null,true);
            } else {
                $this->Session->setFlash(__('The Password could not be saved. Please, try again.',true));
            }
        } else {
            $this->data = $this->User->read(null,$id);
        }
    }

    public function admin_edit($id = null) {
        if (!$this->User->findById($id)) {
            $this->Session->setFlash(__('Invalid user',true));
            $this->redirect(array('action'=>'index'), null, true);
        }
        if(!empty($this->data)) {
            if($this->User->save($this->data['User'])) {
                $this->Session->setFlash(__('The User has been saved',true));
                $this->redirect(array('action'=>'index'),null,true);
            } else {
                $this->Session->setFlash(__('The User could not be saved. Please, try again.',true));
            }
        } else {
            $this->data = $this->User->read(null,$id);
        }
        $this->set('groups',$this->User->Group->find('list'));
    }

    public function admin_delete($id = null) {
        if(!$this->User->findById($id)) {
            $this->Session->setFlash(__('Invalid user',true));
        }
        if ($this->User->del($id)) {
            $this->Session->setFlash(__('User deleted',true));
        } else {
            $this->Session->setFlash(__('The User could not be deleted. Please, try again.',true));
        }
        $this->redirect(array('action'=>'index'),null,true);
    }

    public function admin_login() {
        $this->layout = 'login';
    }

    public function admin_logout() {
        $this->redirect($this->Auth->logout(),null,true);
    }

    public function admin_delete_users() {
        if($this->User->deleteAll(array('User.id'=>$this->data['UsersDelete']))) {
            $this->Session->setFlash(__('Users deleted',true));
        } else {
            $this->Session->setFlash(__('The Users could not be deleted. Please, try again.',true));
        }
        $this->redirect(array('action'=>'index'),null,true);
    }
}
?>